<?php

class NotesController extends AppController {

	function view($delId = null) {
		
		if (!$this->checkSession()) {
			$this->forceLogin();
		}
		
		if(!$this->checkLevel(AUTH_CASH)) {
			$this->Session->setFlash("You can't do that!");
			$this->redirect('/members/menu');
			exit();
		}
		
		App::import('Model', 'Member');
		App::import('Model', 'Producer');
		App::import('Model', 'BasketItem');
		
		if ($delId === null) {
			$delId = $this->deliveryId;
		}
		
		$d = new Delivery();
		$del = $d->findByDeliveryId($delId);
		$neighbors = $d->find('neighbors', array("recursive" => -1, 'field'=>'Delivery.order_open', 'value' => $del['Delivery']['order_open']));
		
		$notes = $this->Note->findAll('delivery_id = ' . $delId);
		
		$m = new Member();
		$p = new Producer();
		$b = new BasketItem();
		
		foreach($notes as $n => $note) {
			switch($note['Note']['table_name']) {
				case "members":
					$notes[$n]['Regarding'] = $m->find('Member.member_id = "' . $note['Note']['table_id'] . '"');
				break;
				case "producers":
					$notes[$n]['Regarding'] = $p->find('Producer.producer_id = "' . $note['Note']['table_id'] . '"');
				break;
				case "basket_items":
					$notes[$n]['Regarding'] = $b->find('BasketItem.id = "' . $note['Note']['table_id'] . '"');
				default:
				break;
			}
		}
		
		$this->set('neighbors', $neighbors);
		$this->set('notes', $notes);
		
	}
	
}

?>